﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Common.LoggingManager;
using System.IO;
using System.Drawing;
using Business.Operations;
using Entity.Framework;

namespace HPDMAS
{
    public class Global : System.Web.HttpApplication
    {
        public int i = 0;
        string errorRawUrl;
        string errorData;
        string errorInnerException;
        string errorMessage;
        string errorSource;
        string errorTargetSite;
        string errorStackTrace;

        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup

        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            Exception ex = Context.Server.GetLastError().GetBaseException();
            Logger.LogException(ex);
            StreamWriter sw;
            //Oluşan hatanın bilgilerini Hatalar.txt adındaki bir dosyaya kaydediyoruz

            try
            {
                sw = new StreamWriter(Server.MapPath("~/HataLog.txt"), true);

                i++;
                sw.Write(i + " - " + DateTime.Now.ToString() + "   ");
                //Request nesnesinin Path özelliği şu an istekte bulunulan sayfanın yol bilgisini getirir
                sw.WriteLine(Request.RawUrl != null ? Request.RawUrl : "");
                //Server nesnesini GetLastError metodu sunucuda oluşan son hatayı Exception tipinden getirir. Bu da şu an oluşan hata olacaktır
                //if (Server.GetLastError().InnerException != null)
                //{
                //    sw.WriteLine(Server.GetLastError().InnerException.Message);
                //}
                //else
                //{
                //    sw.WriteLine(Server.GetLastError().Message);
                //}

                sw.WriteLine("Data           : " + ex.Data);
                sw.WriteLine("HelpLink       : " + ex.HelpLink);
                sw.WriteLine("InnerException : " + ex.InnerException);
                sw.WriteLine("Message        : " + ex.Message);
                sw.WriteLine("Source         : " + ex.Source);
                sw.WriteLine("TargetSite     : " + ex.TargetSite);
                sw.WriteLine("StackTrace     : " + ex.StackTrace);
                sw.WriteLine();
                sw.WriteLine();
                sw.Close();

                //errorRawUrl = Request.RawUrl.ToString();
                //errorData = ex.Data.ToString();
                //errorInnerException = Convert.ToString(ex.InnerException);
                //errorMessage = ex.Message.ToString();
                //errorSource = ex.Source.ToString();
                //errorTargetSite = ex.TargetSite.ToString();
                //errorStackTrace = ex.StackTrace.ToString();

                //ErrorLog errorLog = new ErrorLog();
                //errorLog.ErrorRawUrl = errorRawUrl;
                //errorLog.ErrorData = errorData;
                //errorLog.ErrorInnerException = errorInnerException;
                //errorLog.ErrorMessage = errorMessage;
                //errorLog.ErrorSource = errorSource;
                //errorLog.ErrorTargetSite = errorTargetSite;
                //errorLog.ErrorStackTrace = errorStackTrace;
                //errorLog.ErrorTime = DateTime.Now;
                //ErrorLogOperation.ErrorLogInsert(errorLog);
            }
            catch (Exception ee)
            {
                Response.Redirect("~/HataSayfasi.aspx" + ee.Message);
            }

        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started

        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.

        }

    }
}
